//
//  GuideController.m
//  HealthStarApp
//
//  Created by zhumuzhi on 2019/12/21.
//  Copyright © 2019 zhumuzhi. All rights reserved.
//

#import "GuideController.h"
#import "GuideCell.h"
#import "PageControl.h"

@interface GuideController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) NSArray                       *images;
@property (nonatomic, strong) UICollectionView              *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout    *flowLayout;

@property (nonatomic, strong) UIPageControl                 *pageControl;

@property (nonatomic, strong) UIButton                      *startButton;
@property (nonatomic, strong) UIButton                      *skip;

@end

@implementation GuideController

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.images = @[@"guide1", @"guide2", @"guide3", @"guide4"];
    
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.pageControl];
    [self.view addSubview:self.startButton];
    
    [self.view addSubview:self.skip];
}

#pragma mark - Event
- (void)clickStart {
    if (self.guideEvnetBlock) {
        self.guideEvnetBlock();
    }
    
}

- (void)fastStart {
    if (self.guideEvnetBlock) {
        self.guideEvnetBlock();
    }
    NSLog(@"点击了快速开始按钮");
}



#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GuideCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MZGuideCellID" forIndexPath:indexPath];
    cell.image = self.images[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    self.pageControl.currentPage = round(offset.x / kScreenWidth);
    if (self.pageControl.currentPage == self.images.count - 1) {
        self.startButton.hidden = NO;
    }else {
        self.startButton.hidden = YES;
    }
}
 
#pragma mark - LazyGet

- (UICollectionViewFlowLayout *)flowLayout {
    if (_flowLayout == nil) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight);
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
    }
    return _flowLayout;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:self.flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView setBackgroundColor:[UIColor clearColor]];
        [_collectionView registerClass:[GuideCell class] forCellWithReuseIdentifier:@"MZGuideCellID"];
//        _collectionView.bounces = NO;
        _collectionView.pagingEnabled = YES;
    }
    return _collectionView;
}

- (UIPageControl *)pageControl { // UIPageControl 的默认高度在25左右
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kScreenHeight-170, kScreenWidth, 25)];
        _pageControl.numberOfPages = self.images.count;
        _pageControl.currentPage = 0;
        _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    }
    return _pageControl;
}

- (UIButton *)startButton {
    if (!_startButton) {
        _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _startButton.frame = CGRectMake((kScreenWidth-120) * 0.5, CGRectGetMaxY(self.pageControl.frame) + 20, 120, 44);
        [_startButton setTitle:@"立即体验" forState:UIControlStateNormal];
        [_startButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_startButton addTarget:self action:@selector(clickStart) forControlEvents:UIControlEventTouchUpInside];
        _startButton.layer.borderWidth = 1;
        _startButton.layer.cornerRadius = 22;
        _startButton.titleLabel.font = MZBoldFont(18);
        _startButton.layer.borderColor = [UIColor whiteColor].CGColor;
        _startButton.hidden = YES;
    }
    return _startButton;
}

- (UIButton *)skip {
    if (!_skip) {
        _skip = [UIButton buttonWithType:UIButtonTypeCustom];
        _skip.frame = CGRectMake((kScreenWidth-44) - 10, 60, 44, 44);
        [_skip setTitle:@"跳过" forState:UIControlStateNormal];
        [_skip setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_skip addTarget:self action:@selector(fastStart) forControlEvents:UIControlEventTouchUpInside];
        _skip.layer.borderWidth = 1;
        _skip.layer.cornerRadius = 22;
        _skip.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    return _skip;
}

@end


